package org.xshop.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.xshop.core.JPushHelper;
import org.xshop.mapper.TMemberNotifyMapper;
import org.xshop.model.TMember;
import org.xshop.model.TMemberNotify;
import org.xshop.service.ITMemberNotifyService;

import com.weibo.api.motan.config.springsupport.annotation.MotanService;

import top.ibase4j.core.base.BaseServiceImpl;

/**
 * <p>
 * 会员APP通知  服务实现类
 * </p>
 *
 * @author ShenHuaJie
 * @since 2018-03-01
 */
@MotanService(interfaceClass = ITMemberNotifyService.class)
@CacheConfig(cacheNames = "TMemberNotify")
public class MemberNotifyService extends BaseServiceImpl<TMemberNotify, TMemberNotifyMapper>
implements ITMemberNotifyService {
    @Autowired
    private JPushHelper jpushHelper;

    @Autowired
    private MemberService memberService;

    public void sendNotification(Long memberId, String type, String title, String content) {
        TMember tMember = new TMember();
        tMember.setId(memberId);
        TMember selectOne = memberService.selectOne(tMember);
        Map<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("type", type);
        if (selectOne != null) {
            String registrationId = selectOne.getRegistrationId();
            if (registrationId != null && !registrationId.equals("")) {
                String equipment = registrationId.substring(2, 3);
                try {
                    if ("0".equals(equipment)) {// 安卓
                        jpushHelper.sendNotificationAndroid(title, content, hashMap, registrationId);
                    } else {// ios
                        jpushHelper.sendNotificationIOS(title, content, hashMap, registrationId);
                    }
                    TMemberNotify memberNotify = new TMemberNotify();
                    memberNotify.setMessageType(type);
                    memberNotify.setMemberId(memberId);
                    memberNotify.setMessageInfo(content);
                    memberNotify.setMessageTitle(title);
                    memberNotify.setStatus("0");
                    super.update(memberNotify);
                } catch (Exception e) {
                    logger.error("发送通知失败", e);
                }
            } else {
                logger.warn("sendNotification no registrationId : " + memberId);
            }
        }
    }
}
